package com.kefu.robot.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kefu.chat.model.ChatSession;
import com.kefu.chat.vo.LikeAndAtepOnDetialVO;
import com.kefu.chat.vo.LikeAndAtepOnResultVO;
import com.kefu.chat.vo.LikeAndAtepOnSeachVO;
import com.kefu.chat.vo.likeAndAtepOnDetialResult;
import com.kefu.common.context.KefuContext;
import com.kefu.common.service.AbstractKefuService;
import com.kefu.robot.mapper.RobotDocCommentMapper;
import com.kefu.robot.mapper.RobotDocMapper;
import com.kefu.robot.mapper.RobotInfoMapper;
import com.kefu.robot.model.*;
import icu.mhb.mybatisplus.plugln.core.JoinLambdaWrapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class RobotDocCommentService extends AbstractKefuService<RobotDocComment> {

    @Autowired
    private RobotDocCommentMapper robotDocCommentMapper;

    @Autowired
    private RobotInfoMapper robotInfoMapper;

    @Autowired
    private RobotDocMapper robotDocMapper;


    public Page<LikeAndAtepOnResultVO> likeAndAtepOn(LikeAndAtepOnSeachVO likeAndAtepOnSeachVO) {
        Page<LikeAndAtepOnResultVO> page = new Page<LikeAndAtepOnResultVO>(likeAndAtepOnSeachVO.getPageNo(), likeAndAtepOnSeachVO.getPageSize(), true);
        //创建条件构造器
        QueryWrapper<RobotDocComment> wrapper = new QueryWrapper<RobotDocComment>();
        wrapper.select("count(*) as count,doc_id");
        wrapper.groupBy("doc_id");
        if (StringUtils.isNotBlank(String.valueOf(likeAndAtepOnSeachVO.getStartTime())) && StringUtils.isNotBlank(String.valueOf(likeAndAtepOnSeachVO.getEndTime()))) {
            wrapper.lt("create_time", likeAndAtepOnSeachVO.getEndTime());
            wrapper.ge("create_time", likeAndAtepOnSeachVO.getStartTime());
            wrapper.eq("is_useful", likeAndAtepOnSeachVO.getLikeAndAtepOn());
        }
        wrapper.eq("tenant_id", KefuContext.getKefuContext().getTenantId());
        List<RobotDocComment> robotDocCommentPage = robotDocCommentMapper.selectList(wrapper);
        Page<LikeAndAtepOnResultVO> result = null;
        if (!robotDocCommentPage.isEmpty()) {
            //联表查询出分类，问题
            JoinLambdaWrapper<RobotDoc> joinLambdaWrapper = new JoinLambdaWrapper<>(RobotDoc.class);
            joinLambdaWrapper.select(RobotDoc::getDocId, RobotDoc::getDocQuestion, RobotDoc::getDocTypeId);
            robotDocCommentPage.forEach(robotDocComment -> {
                joinLambdaWrapper.eq(RobotDoc::getDocId, robotDocComment.getDocId())
                        .or();
            });
            joinLambdaWrapper.leftJoin(RobotDocType.class, RobotDocType::getId, RobotDoc::getDocTypeId)
                    .selectAs(RobotDocType::getName, "docTypeName")
                    .end();
            Page<LikeAndAtepOnResultVO> likeAndAtepOnResultVOPage = robotDocMapper.joinSelectPage(page, joinLambdaWrapper, LikeAndAtepOnResultVO.class);
            //处理数据
            robotDocCommentPage.forEach(robotDocComment -> {
                likeAndAtepOnResultVOPage.getRecords().forEach(robotDoc -> {
                    if (robotDocComment.getDocId().equals(robotDoc.getDocId())) {
                        robotDoc.setCount(robotDocComment.getCount());
                    }
                });
            });
            result = likeAndAtepOnResultVOPage;
        }
        return result;
    }

    public Page<likeAndAtepOnDetialResult> likeAndAtepOnDetial(LikeAndAtepOnDetialVO likeAndAtepOnDetialVO) {
        Page<likeAndAtepOnDetialResult> page = new Page<likeAndAtepOnDetialResult>(likeAndAtepOnDetialVO.getPageNo(), likeAndAtepOnDetialVO.getPageSize(), true);
        JoinLambdaWrapper<RobotDocComment> wrapper = new JoinLambdaWrapper<>(RobotDocComment.class);
        wrapper.select(RobotDocComment::getSessionId, RobotDocComment::getCreateTime, RobotDocComment::getDocId)
                .eq(RobotDocComment::getTenantId, KefuContext.getKefuContext().getTenantId())
                .eq(RobotDocComment::getDocId, likeAndAtepOnDetialVO.getDocId())
                .eq(RobotDocComment::getIsUseful, likeAndAtepOnDetialVO.getLikeAndAtepOn());
        if ("createTime".equals(likeAndAtepOnDetialVO.getOrderField())) {
            if ("ASC".equals(likeAndAtepOnDetialVO.getOrder().name())) {
                wrapper.orderBy(true, true, RobotDocComment::getCreateTime);
            }
            if ("DESC".equals(likeAndAtepOnDetialVO.getOrder().name())) {
                wrapper.orderBy(true, false, RobotDocComment::getCreateTime);
            }
        }
        wrapper.leftJoin(ChatSession.class, ChatSession::getSessionId, RobotDocComment::getSessionId)
                .select(ChatSession::getUserName, ChatSession::getUserId)
                .end();

        Page<likeAndAtepOnDetialResult> robotDocCommentList = robotDocCommentMapper.joinSelectPage(page, wrapper, likeAndAtepOnDetialResult.class);
        return robotDocCommentList;
    }
}
